
//
//  AdHeaderView.swift
//  Automobile
//
//  Created by erkebulan elzhan on 7/27/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit

protocol AdHeaderViewDelegate {
    func headerButtonPressed ()
}

final class AdHeaderView: UIView {
    
    // MARK: - Outlets
    
    lazy private var titleLbl: UILabel = {
        let lbl = UILabel()
        lbl.font = UIFont.systemFont(ofSize: 24, weight: .bold)
        lbl.textColor = .black
        lbl.sizeToFit()
        return lbl
    }()
    
    lazy private var plusBtn: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "plus"), for: .normal)
        btn.contentEdgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
        return btn
    }()
    
    // MARK: - Properties
    
    var delegate: AdHeaderViewDelegate!
    
    // MARK: - UI actions
    
    @objc private func onAddClick (sender: UIButton) {
        delegate.headerButtonPressed()
    }
    
    
    // MARK: - Init
    
    init (customIcon: UIImage? = UIImage(named: "plus")) {
        super.init(frame: .zero)
        self.titleLbl.text = "Объявления"
        self.addUIConstraints(customIcon: customIcon)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    
    private func addUIConstraints (customIcon: UIImage?) {
        self.addSubview(titleLbl)
        self.addSubview(plusBtn)
        
        plusBtn.addTarget(self, action: #selector(onAddClick(sender:)), for: .touchUpInside)
        
        plusBtn.snp.makeConstraints { (make) in
            make.width.height.equalTo(26)
            make.centerY.equalTo(titleLbl)
            make.trailing.equalToSuperview().inset(16)
        }
        titleLbl.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().inset(22)
            make.leading.equalToSuperview().offset(16)
        }
        
        plusBtn.setImage(customIcon, for: .normal)
    }
}
